﻿using System.Collections.Concurrent;
using KnownCMS.Entities;

namespace WebSite;

/// <summary>
/// 内容系统身份认证服务类。
/// </summary>
public class AuthService
{
    // 缓存登录用户
    private static readonly ConcurrentDictionary<string, UserInfo> Users = new();
    // 缓存微信登录状态
    private static readonly ConcurrentDictionary<string, Result> Tokens = new();

    /// <summary>
    /// 获取微信登录二维码Token。
    /// </summary>
    /// <returns>二维码Token。</returns>
    public static string GetWeixinQRCodeToken()
    {
        var token = Utils.GetGuid();
        Tokens[token] = Result.Error("等待扫码登录！");
        return token;
    }

    /// <summary>
    /// 检查微信扫码结果。
    /// </summary>
    /// <param name="token">二维码Token。</param>
    /// <returns>微信扫码结果。</returns>
    public static Result CheckWeixinQRCodeToken(string token)
    {
        if (!Tokens.ContainsKey(token))
            return Result.Error("二维码票据不存在！");

        return Tokens[token];
    }

    /// <summary>
    /// 异步获取用户信息。
    /// </summary>
    /// <param name="userName">用户登录名。</param>
    /// <returns>用户信息。</returns>
    public static async Task<UserInfo> GetUserAsync(string userName)
    {
        if (string.IsNullOrWhiteSpace(userName))
            return null;

        Users.TryGetValue(userName, out UserInfo info);
        if (info == null)
        {
            var database = Database.Create();
            var user = await database.QueryAsync<CmUser>(d => d.UserName == userName);
            if (user != null)
            {
                info = GetUserInfo(user);
                Users[info.UserName] = info;
            }
        }
        return info;
    }

    /// <summary>
    /// 异步用户登录。
    /// </summary>
    /// <param name="info">登录信息。</param>
    /// <returns>登录结果。</returns>
    public static async Task<Result> LoginAsync(LoginInfo info)
    {
        if (!info.IsPassword) //微信扫码注册或登录
        {
            var result = Result.Success("");
            Tokens[info.Token] = result;
            return result;
        }

        var database = Database.Create();
        var user = await database.QueryAsync<CmUser>(d => d.UserName == info.UserName);
        if (user == null)
            return Result.Error("用户名不存在！");

        var password = Utils.ToMd5(info.Password);
        if (user.Password != password)
            return Result.Error("密码不正确！");

        var data = GetUserInfo(user);
        Users[info.UserName] = data;
        return Result.Success("登录成功！", data);
    }

    private static UserInfo GetUserInfo(CmUser user)
    {
        var info = new UserInfo
        {
            Id = user.Id,
            AppId = user.AppId,
            CompNo = user.CompNo,
            UserName = user.UserName,
            Name = user.NickName,
            Gender = user.Sex,
            AvatarUrl = user.AvatarUrl,
            Role = user.Metier,
            Token = Utils.GetGuid()
        };
        return info;
    }
}